#
#  octopus4r - a ruby wrapper for the Octopus USB board
#  Copyright (C) 2008 by Michael Conrad <do5mc@friggleware.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#


require 'octopus_native'


module Octopus4r

  class Port

    def initialize( device, port)
      @device = device
      @port = port.to_i

      return
    end


    def init
      Octopus_native::octopus_io_init_port( @device, @port)
    end


    def value 
      Octopus_native::octopus_io_get_port( @device, @port)
    end

    def value=(value)
      Octopus_native::octopus_io_set_port( @device, @port, value.to_i)
    end


    def direction=(value)

      case value 
      when PORT_DIRECTION_IN
        Octopus_native::octopus_io_set_port_direction_in( @device, @port, 0xFF)
      when PORT_DIRECTION_OUT
        Octopus_native::octopus_io_set_port_direction_out( @device, @port, 0xFF)
      when PORT_DIRECTION_TRISTATE
        Octopus_native::octopus_io_set_port_direction_tri( @device, @port, 0xFF)
      else
        raise "wrong port direction value"
      end
      
    end

  end


  class Pin

    def initialize( device, pin)
      @device = device
      @pin = pin.to_i

      return
    end
      
    def init 
      Octopus_native::octopus_io_init( @device, @pin)
    end

    def value 
      Octopus_native::octopus_io_get_pin( @device, @pin)
    end

    def value=(value)
      Octopus_native::octopus_io_set_pin( @device, @pin, value.to_i)
    end


    def direction=(value)
      
      case value
      when PIN_DIRECTION_IN
        Octopus_native::octopus_io_set_pin_direction_in( @device, @pin)
      when PIN_DIRECTION_OUT
        Octopus_native::octopus_io_set_pin_direction_out( @device, @pin)
      when PIN_DIRECTION_TRISTATE
        Octopus_native::octopus_io_set_pin_direction_tri( @device, @pin)
      else
        raise "wrong pin direction value"
      end
          
    end
      
  end
  
end
